import { request } from '@umijs/max';

/* 登录 */
export async function loginApi(body: any, options?: { [key: string]: any }) {
  const formData = new FormData();

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<any>('/user/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data: body,
    ...(options || {}),
  });
}

/* 获取验证码 */
export async function getCaptchaApi(
  body: {
    mobile: string;
  },
  options?: { [key: string]: any },
) {
  return request<any>('/common/code', {
    method: 'GET',
    params: body,
    ...(options || {}),
  });
}

// 获取审核列表
export async function getAuditListApi(body) {
  return request<any>('/program/list', {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 审核列表删除
export async function delectAuditDeleteApi(body) {
  return request<any>(`/program/list/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}

// 审核列表审核通过
export async function delectAuditPassApi(body) {
  return request<any>(`/program/list/auditing/${body?.id}`, {
    method: 'PUT',
    params: body,
    // ...(options || {}),
  });
}

// 审核列表驳回
export async function delectAuditRejectApi(body) {
  return request<any>(`/program/list/reject/${body?.id}`, {
    method: 'PUT',
    params: body,
    // ...(options || {}),
  });
}

//商户清单列表
export async function getMerchantListApi(body) {
  return request<any>(`/program/inventory`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 修改商户清单
export async function updateMerchantListApi(id, body) {
  return request<any>(`/program/list/${id}`, {
    method: 'PUT',
    data: body,
  });
}

// 个人信息
export async function getUserInfoApi() {
  return request<any>('/user/detail', {
    method: 'GET',
    // params: body,
    // ...(options || {}),
  });
}

// 绑定规则列表
export async function getBindRuleListApi(body) {
  return request<any>(`/battery/binding`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

/* 公告 */
export async function getNoticeListApi(body) {
  return request<any>(`/board/notice`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

/* 新增公告 */
export async function addNoticeApi(body) {
  return request<any>(`/board/notice`, {
    method: 'POST',
    data: body,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 新增绑定规则
export async function addBindRuleApi(body) {
  return request<any>(`/battery/binding`, {
    method: 'POST',
    data: body,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 删除规则绑定吧
export async function delectBindRuleApi(body) {
  return request<any>(`/battery/binding/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}
/* 删除公告 */
export async function delectNoticeApi(body) {
  return request<any>(`/board/notice/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}

/* 修改公告 */
export async function updateNoticeApi(id, body) {
  return request<any>(`/board/notice/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 修改绑定规则
export async function updateBindRuleApi(id, body) {
  return request<any>(`/battery/binding/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 优惠券列表
export async function getCouponListApi(body) {
  return request<any>(`/battery/coupon-rule`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

export async function addCoupon(body) {
  return request<any>(`/battery/coupon-rule`, {
    method: 'POST',
    data: body,
    headers: {
      'Content-Type': 'application/json',
    },
    // ...(options || {}),
  });
}

export async function updateCouponRuleApi(id, body) {
  return request<any>(`/battery/coupon-rule/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 删除
export async function delectCouponApi(body) {
  return request<any>(`/battery/coupon-rule/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}

// 商户配置项
export async function getProgramOptions() {
  return request<any>(`/program/select`, {
    method: 'GET',
    // ...(options || {}),
  });
}

// 优惠券日志接口
export async function getCouponLog(body) {
  return request<any>(`/journal/coupons`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 发展用户列表
export async function getDevelopUserList(body) {
  return request<any>(`/battery/develop-list`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 在租用户
export async function getRentUserList(body) {
  return request<any>(`/battery/renting-list`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 逾期
export async function getOverdueUserList(body) {
  return request<any>(`/battery/overdue-list`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 在租
export async function importRentUserList(body) {
  const formData = new FormData();

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  console.log('formData', formData);

  return request<any>(`/battery/import-excel`, {
    method: 'POST',
    data: formData,
    // ...(options || {}),
  });
}

// 批量转换
export async function batchconvert(body) {
  const formData = new FormData();

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  console.log('formData', formData);

  return request<any>(`/battery/batch-convert`, {
    method: 'POST',
    data: formData,
    // ...(options || {}),
  });
}

export async function importDevelopUserList(body) {
  const formData = new FormData();

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<any>(`/battery/import-develop`, {
    method: 'POST',
    data: formData,
    // ...(options || {}),
  });
}

// 发优惠券日志
export async function getSendCouponLog(body) {
  return request<any>(`/journal/coupons-log`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 渠道转个人日志
export async function getChannelLog(body) {
  return request<any>(`/journal/all-log`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 校验日志
export async function getCheckLog(body) {
  return request<any>(`/journal/check-log`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 绑定日志
export async function getBindLog(body) {
  return request<any>(`/journal/binding-log`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 电池查询日志
export async function getBatteryLog(body) {
  return request<any>(`/journal/battery-log`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 数据上传日志
export async function getUploadLog(body) {
  return request<any>(`/journal/data-log`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 发展用户规则
export async function getDevelopRuleList(body) {
  return request<any>(`/finance/development-rules`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 修改
export async function updateDevelopRuleApi(id, body) {
  return request<any>(`/finance/development-rules/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 新增
export async function addDevelopRuleApi(body) {
  return request<any>(`/finance/development-rules`, {
    method: 'POST',
    data: body,
    // ...(options || {}),
  });
}

// 删除
export async function delectDevelopRuleApi(id) {
  return request<any>(`/apifinance/development-rules/${id}`, {
    method: 'DELETE',
    // ...(options || {}),
  });
}

// 提现记录
export async function withdrawListApi(params) {
  return request<any>(`/finance/withdrawal`, {
    method: 'GET',
    params: params,
    // ...(options || {}),
  });
}

// 打款成功
export async function paySuccessApi(id) {
  // /finance/withdrawal/success/1
  return request<any>(`/finance/withdrawal/success/${id}`, {
    method: 'POST',
    // data: params,
    // ...(options || {}),
  });
}

// 打款失败
export async function payFailApi(id) {
  return request<any>(`/finance/withdrawal/failed/${id}`, {
    method: 'POST',
    // data: params,
    // ...(options || {}),
  });
}

/* 商户配置列表 */
export async function getProgramListApi(body) {
  return request<any>(`/program/user-config`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

/* 获取权限列表 */
export async function getPermissionListApi() {
  return request<any>(`/authority/permission`, {
    method: 'GET',
    // params: body,
    // ...(options || {}),
  });
}

/* 给商户权限 */
export async function givePermissionApi(id, body) {
  return request<any>(`/authority/set/${id}`, {
    method: 'POST',
    data: body,
    // ...(options || {}),
  });
}

// 给商户绑定规则
export async function postAutohorityApi(id, body) {
  return request<any>(`/authority/binding/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 给商户配置优惠券
export async function postCouponApi(id, body) {
  return request<any>(`/battery/coupon-config/${id}`, {
    method: 'POST',
    data: body,
    // ...(options || {}),
  });
}

// 优惠券列表
export async function getSotreCouponListApi(id, body) {
  return request<any>(`/battery/coupon-config/${id}`, {
    method: 'GET',
    data: body,
    // ...(options || {}),
  });
}

// 给商户设置奖励
export async function postAutohorityRewardApi(id, body) {
  return request<any>(`/authority/set/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

/* 获取权限码列表 */
export async function getPermissionCodeListApi() {
  return request<any>(`/authority/permission`, {
    method: 'GET',
    // params: body,
    // ...(options || {}),
  });
}

/* 删除 */
export async function delectPermissionApi(id) {
  return request<any>(`/authority/permission/${id}`, {
    method: 'DELETE',
    // params: body,
    // ...(options || {}),
  });
}

/* 保存 */
export async function savePermissionApi(body) {
  return request<any>(`/authority/permission`, {
    method: 'POST',
    data: body,
    // ...(options || {}),
  });
}

/* 修改 */
export async function updatePermissionApi(id, body) {
  return request<any>(`/authority/permission/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

/* 获取经纬度 */
export async function getLocationApi(body) {
  return request<any>(`/battery/develop-look`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 删除删除
export async function deleteDevelopApi(id) {
  return request<any>(`/battery/develop/${id}`, {
    method: 'DELETE',
    // ...(options || {}),
  });
}

// 获取配置详情
export async function getConfigDetailApi(id) {
  return request<any>(`/user/config/${id}`, {
    method: 'GET',
    // ...(options || {}),
  });
}

// 修改配置
export async function updateConfigApi(id, body) {
  return request<any>(`/user/config/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 导出
export async function exportExcelApi(body) {
  return request<any>(`/journal/all-export`, {
    method: 'POST',
    data: body,
    responseType: 'arraybuffer',
  });
}

// 权限列表
export async function getUserPermissionListApi(body) {
  return request<any>(`/authority/permissions-tree`, {
    method: 'GET',
    data: body,
  });
}

// 新增用户权限
export async function addUserPermissionApi(body) {
  return request<any>(`/authority/permissions`, {
    method: 'POST',
    data: body,
  });
}

// 删除用户权限
export async function deleteUserPermissionApi(id) {
  return request<any>(`/authority/permissions/${id}`, {
    method: 'DELETE',
  });
}

// 修改用户权限
export async function updateUserPermissionApi(id, body) {
  return request<any>(`/authority/permissions/${id}`, {
    method: 'PUT',
    data: body,
  });
}

// 角色列表
export async function getUserRoleListApi(body) {
  return request<any>(`/authority/roles`, {
    method: 'GET',
    data: body,
  });
}

// 新增角色
export async function addUserRoleApi(body) {
  return request<any>(`/authority/roles`, {
    method: 'POST',
    data: body,
  });
}

// 编辑角色
export async function updateUserRoleApi(id, body) {
  return request<any>(`/authority/roles/${id}`, {
    method: 'PUT',
    data: body,
  });
}

// 删除角色
export async function deleteUserRoleApi(id) {
  return request<any>(`/authority/roles/${id}`, {
    method: 'DELETE',
  });
}

// 用户列表
export async function getUserListApi(body) {
  return request<any>(`/user/operation`, {
    method: 'GET',
    data: body,
  });
}

// 删除角色
export async function deleteRoleUserApi(id) {
  return request<any>(`/user/operation/${id}`, {
    method: 'DELETE',
  });
}

// 新增用户
export async function addUserApi(body) {
  return request<any>(`/user/operation`, {
    method: 'POST',
    data: body,
  });
}

// 编辑
export async function updateUserApi(id, body) {
  return request<any>(`/user/operation/${id}`, {
    method: 'PUT',
    data: body,
  });
}

// 删除
export async function deleteUserApi(id) {
  return request<any>(`/user/operation/${id}`, {
    method: 'DELETE',
  });
}

// 注册
export async function registerApi(body) {
  return request<any>(`/user/operation`, {
    method: 'POST',
    data: body,
  });
}

// 给角色配置权限
export async function setRolePermissionApi(id, body) {
  return request<any>(`/authority/roles-set/${id}`, {
    method: 'POST',
    data: body,
  });
}


export async function exportdevelopingExcelApi(body) {
  return request<any>(`/battery/develop-export`, {
    method: 'POST',
    data: body,
    responseType: 'arraybuffer',
  });
}

/* /battery/quantity-export */
export async function exportQuantityExcelApi(body) {
  return request<any>(`/battery/quantity-export`, {
    method: 'POST',
    data: body,
    responseType: 'arraybuffer',
  });
}

/* 删除数据 */
export async function deleteDevelopingApi() {
  return request<any>(`/battery/clear-consumer`, {
    method: 'POST',
    // ...(options || {}),
  });
}